#include <iostream>
#include <map>
#include <utility>
#include <string>

using std::cout;
using std::endl;
using std::multimap;
using std::pair;
using std::make_pair;
using std::string;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
}

void test()
{
    //multimap的基本特征
    //1、存放的是key-value类型（也就是pair类型），key值是不唯一的，可以
    //重复，value值既可以重复也可以不重复
    //2、默认会按照key值进行升序排列
    //3、底层使用的还是红黑树
    multimap<int, string> number = {
        {1, "hello"},//1、直接使用大括号构建pair
        {4, "hello"},
        make_pair(3, "beijing"),//2、使用make_pair的返回结果构建pair类型
        make_pair(7, "nanjing"),
        pair<int, string>(4, "wangdao"),//3、使用pair的构建函数构建pair
        pair<int, string>(5, "dongjing"),
    };
    display(number);

    cout << endl << "multimap的查找" << endl;
    size_t cnt = number.count(3);
    cout << "cnt = " << cnt << endl;

    multimap<int, string>::iterator it = number.find(9);
    if(it != number.end())
    {
        cout << "元素是存在的 " 
             << it->first << "  "
             << it->second << endl;
    }
    else
    {
        cout << "该元素不存在multimap中" << endl;
    }

    cout <<endl << "multimap的insert操作" << endl;
    number.insert({9, "wangdao"});
    /* number.insert(make_pair(9, "wangdao")); */
    /* number.insert(pair<int, string>(9, "wangdao")); */
    display(number);

    cout << endl << "multimap的删除操作" << endl;
    auto it2 = number.begin();
    number.erase(it2);
    display(number);

#if 0
    cout << endl << "multimap的下标" << endl;
    cout << "number[3] = " << number[3] << endl;//查找
    cout << "number[1] = " << number[1] << endl;//插入
    display(number);

    cout << endl;
    //T &operator[](const Key &key)
    number[1] = "hello";//修改
    display(number);

    const multimap<int, string> tmp = { {1, "wangdao"} };
    //multimap没有重载const版本的operator[]
    /* cout << "tmp[1] = " << tmp[1] << endl;//error, */
#endif
}


int main(int argc, char *argv[])
{
    test();
    return 0;
}

